package com.frxuexi.park2210

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.navigation.findNavController
import androidx.navigation.ui.NavigationUI
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.LiveDataBus
import com.frxuexi.lib_base.net.Const
import com.frxuexi.park2210.databinding.ActivityMainBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Route(path = "/app/MainActivity")
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    lateinit var binding: ActivityMainBinding
    var myname:String?=null
    var mypass:String="666"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding=ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
//        ARouter.getInstance().build("/shop/ShopActivity").navigation()
        val navController = findNavController(R.id.fragment_main)
        NavigationUI.setupWithNavController(binding.bnv,navController)

        binding.mainHeader.change.apply {
            visibility=View.VISIBLE
            setOnClickListener {
                SPUtils.getInstance().remove(Const.TOKEN)
                SPUtils.getInstance().remove(Const.UID)
                SPUtils.getInstance().remove(Const.UNAME)
                ARouter.getInstance().build("/app/LoginActivity").navigation()
            }
        }

        EventBus.getDefault().register(this)

        LiveDataBus.with("message").postValue("LiveData消息")

        myname?.let { Log.i("===", it) }
        if(myname!=null){
            Log.i("===", myname!!)
        }
        Log.i("===",mypass)

        lifecycleScope.launch {
            flow<String> {
                delay(2000)
                emit("hello")
            }.collect{
                Log.i("===",it)
            }
        }


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun getMsg(msg:String){
        ToastUtils.showLong(msg)
    }
}